<?php


namespace app\controller\admin;


use app\BaseController;
use app\model\admin\AdminRoleModel;
use app\service\admin\AdminUserService;
use app\service\config\ConfigService;
use app\service\utils\CaptchaService;
use app\service\utils\TokenService;
use think\Request;

class IndexController extends BaseController
{

    /**
     * 获取验证码
     * @return \think\response\Json
     */
    public function getCaptchaCode()
    {
        try {
            $data = CaptchaService::createCaptcha();
            return $this->success($data);
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }

    /**
     * 网站配置信息
     * @return \think\response\Json
     */
    public function getWebConfig()
    {
        try {
            $configInfo = ConfigService::getConfigValueArray(['logo', 'site_title', 'login_img', 'favicon_img', 'company_no']);
            return $this->success($configInfo);
        } catch (\Exception $exception) {
            return $this->error($exception);
        }

    }

    /**
     * 登录
     * @param Request $request
     * @return \think\response\Json
     */
    public function login(Request $request)
    {
        try {
            $username = $request->param('username');
            $password = $request->param('password');
            $uniqid = $this->request->param('uniqid', '', 'trim');
            $imgCode = $this->request->param('img_code', '', 'trim');
            // 图片验证码

            CaptchaService::verifyCaptcha($uniqid, $imgCode);

            $adminUserService = new AdminUserService();
            $userInfo = $adminUserService->getHiddenOneLineByKey('username', $username);
            if (!$userInfo) {
                throw new \Exception('用户不存在');
            }
            if (!password_verify($password, $userInfo['password'])) {
                throw new \Exception('账号密码错误');
            }
            if ($userInfo['status'] == 1) {
                throw new \Exception('账号已被禁用');
            }
            // 查询角色状态
            $roleInfo = AdminRoleModel::find($userInfo['role_id']);
            if (!$roleInfo || $roleInfo['is_del']) {
                throw new \Exception('角色已禁用');
            }
            //更新token 和 过期时间
            $data = [
                'id' => $userInfo['id'],
                'role_id' => $userInfo['role_id'],
                'avatar' => $userInfo['avatar'],
                'username' => $username,
            ];
            $tokenService = new TokenService();
            $token = $tokenService->generateToken($data);
            $data['token'] = $token;
            return $this->success($data);
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }

    public function getInfo()
    {
        $data = [
//            'today_new_user' => $todayNewUser,
            'today_order_count' => 0,
            'today_order_amount' => 0,
            'tech_money_count' => 0 + 0,
            'user_money_count' => 0,
//            'order_amount' => $orderAmount,
            'total_withdraw' => 0
        ];
        return $this->success($data);
    }
}